/* 
    WhaleSpotter
    Copyright (C) 2007  Bob Mottram
    fuzzgun@gmail.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace whalespotter
{
    public class whaleMap
    {
        public String filename;
        public String description = "";
        public float top_longitude;
        public float top_lattitude;
        public float bottom_longitude;
        public float bottom_lattitude;

        public whaleMap()
        {
        }

        public whaleMap(String filename, 
                        float top_longitude, float top_lattitude,
                        float bottom_longitude, float bottom_lattitude)
        {
            this.filename = filename;
            this.top_longitude = top_longitude;
            this.top_lattitude = top_lattitude;
            this.bottom_longitude = bottom_longitude;
            this.bottom_lattitude = bottom_lattitude;
        }

        public void Save(BinaryWriter binfile)
        {
            binfile.Write(filename);
            binfile.Write(description);
            binfile.Write(top_longitude);
            binfile.Write(top_lattitude);
            binfile.Write(bottom_longitude);
            binfile.Write(bottom_lattitude);
        }

        public void Load(BinaryReader binfile)
        {
            filename = binfile.ReadString();
            description = binfile.ReadString();
            top_longitude = binfile.ReadSingle();
            top_lattitude = binfile.ReadSingle();
            bottom_longitude = binfile.ReadSingle();
            bottom_lattitude = binfile.ReadSingle();
        }
    }
    
    public class whaleMaps
    {
        ArrayList maps = new ArrayList();

        /// <summary>
        /// returns a list of map filenames
        /// </summary>
        /// <returns></returns>
        public ArrayList getFilenames()
        {
            ArrayList filenames = new ArrayList();
            for (int i = 0; i < maps.Count; i++)
            {
                whaleMap m = (whaleMap)maps[i];
                filenames.Add(m.filename);
            }
            return (filenames);
        }

        /// <summary>
        /// get a list of map descriptions
        /// </summary>
        /// <returns></returns>
        public ArrayList getDescriptions()
        {
            ArrayList descriptions = new ArrayList();
            for (int i = 0; i < maps.Count; i++)
            {
                whaleMap m = (whaleMap)maps[i];
                descriptions.Add(m.description);
            }
            return (descriptions);
        }


        /// <summary>
        /// returns the description associated with the given map
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public String getMapDescription(String filename)
        {
            String description = "";
            int index = getMapIndex(filename);
            if (index > -1)
            {
                whaleMap m = (whaleMap)maps[index];
                description = m.description;
            }
            return(description);
        }


        /// <summary>
        /// save a set of maps to file
        /// </summary>
        /// <param name="filename"></param>
        public void Save(String filename)
        {
            FileStream fp = new FileStream(filename, FileMode.Create);
            BinaryWriter binfile = new BinaryWriter(fp);

            binfile.Write(maps.Count);
            for (int i = 0; i < maps.Count; i++)
            {
                whaleMap m = (whaleMap)maps[i];
                m.Save(binfile);
            }

            binfile.Close();
            fp.Close();
        }

        /// <summary>
        /// load a set of maps from file
        /// </summary>
        /// <param name="filename"></param>
        public void Load(String filename)
        {
            if (File.Exists(filename))
            {
                FileStream fp = new FileStream(filename, FileMode.Open);
                BinaryReader binfile = new BinaryReader(fp);

                maps.Clear();
                int no_of_maps = binfile.ReadInt32();
                for (int i = 0; i < no_of_maps; i++)
                {
                    whaleMap m = new whaleMap();
                    m.Load(binfile);
                    if (File.Exists(m.filename))
                        maps.Add(m);
                }

                binfile.Close();
                fp.Close();
            }
        }

        /// <summary>
        /// return the index of the map with the given filename
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public int getMapIndex(String filename)
        {
            int index = -1;

            int i = 0;
            while ((i < maps.Count) && (index == -1))
            {
                whaleMap m = (whaleMap)maps[i];
                if (m.filename == filename) index = i;
                i++;
            }
            return (index);
        }

        public String getMapFilename(int index)
        {
            String filename = "";
            if ((index > -1) && (index < maps.Count))
            {
                whaleMap m = (whaleMap)maps[index];
                filename = m.filename;
            }
            return (filename);
        }

        /// <summary>
        /// remove a map
        /// </summary>
        /// <param name="filename"></param>
        public void Remove(String filename)
        {
            int index = getMapIndex(filename);
            if (index > -1)
            {
                maps.RemoveAt(index);
            }
        }

        /// <summary>
        /// return the filename associated with the given description
        /// </summary>
        /// <param name="description"></param>
        /// <returns></returns>
        public String getMapFilename(String description)
        {
            int index = -1;
            String filename = "";

            int i = 0;
            while ((i < maps.Count) && (index == -1))
            {
                whaleMap m = (whaleMap)maps[i];
                if (m.description == description)
                {
                    index = i;
                    filename = m.filename;
                }
                i++;
            }
            return (filename);
        }

        /// <summary>
        /// add a new map with the given filename
        /// </summary>
        /// <param name="filename"></param>
        public void Add(String filename)
        {
            if (getMapIndex(filename) == -1)
            {
                whaleMap m = new whaleMap(filename, 0, 0, 0, 0);
                m.description = "Map " + Convert.ToString(maps.Count+1);
                maps.Add(m);
            }
        }

        /// <summary>
        /// sets parameters for the map with the given filename
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="description"></param>
        /// <param name="top_longitude"></param>
        /// <param name="top_lattitude"></param>
        /// <param name="bottom_longitude"></param>
        /// <param name="bottom_lattitude"></param>
        public void setMapParameters(String filename, String description,
                                     String top_longitude, String top_lattitude,
                                     String bottom_longitude, String bottom_lattitude)
        {
            int index = getMapIndex(filename);
            if (index > -1)
            {
                whaleMap m = (whaleMap)maps[index];
                m.description = description;
                m.top_longitude = util.MapToFloat(top_longitude);
                m.top_lattitude = util.MapToFloat(top_lattitude);
                m.bottom_longitude = util.MapToFloat(bottom_longitude);
                m.bottom_lattitude = util.MapToFloat(bottom_lattitude);
            }
        }

        /// <summary>
        /// get parameters for the given filename
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="description"></param>
        /// <param name="top_longitude"></param>
        /// <param name="top_lattitude"></param>
        /// <param name="bottom_longitude"></param>
        /// <param name="bottom_lattitude"></param>
        public void getMapParameters(String filename, ref String description,
                                     ref String top_longitude, ref String top_lattitude,
                                     ref String bottom_longitude, ref String bottom_lattitude)
        {
            int index = getMapIndex(filename);
            if (index > -1)
            {
                whaleMap m = (whaleMap)maps[index];
                description = m.description;
                top_longitude = util.FloatToMap(m.top_longitude, true);
                top_lattitude = util.FloatToMap(m.top_lattitude, false);
                bottom_longitude = util.FloatToMap(m.bottom_longitude, true);
                bottom_lattitude = util.FloatToMap(m.bottom_lattitude, false);
            }
        }

    }
}
